<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style type="text/css">
        .container{
            width: 400px;
            height: 300px;
            margin: 0 auto;
            border: 3px solid red;
        }
        .tabs>span{
            float: left;
            width: 100px;
            height: 50px;
            line-height: 50px;
            text-align: center;
        }
        .contents{
            clear: both;
        }
        .contents>div{
            height: 250px;
            line-height: 250px;
            font-size: 50px;
            text-align: center;
            display: none;
        }
        .contents>.show{
            display: block;
        }
        .active{
            background-color: #cdcdcd;
        }
    </style>


</head>
<body>
<div class="container">
    <div class="tabs">
        <span class="active">导航0</span>
        <span>导航1</span>
        <span>导航2</span>
        <span>导航3</span>
    </div>
    <div class="contents">
        <div class="show">导航0内容</div>
    </div>
</div>

<!--IIFE 解决变量共享问题的实例-->
<script>
    var span = document.getElementsByTagName('span');
    var show = document.getElementsByClassName('show')[0];
    for(var i = 0 ; i < span.length ; i++)
    {
        //通过使用IIFE模式，将每个i值都放到一个独立的空间，就避免了变量共用
        (function (j){
            span[j].onclick = function(){
            show.innerHTML = "导航"+j+'内容';
            span[j].style.backgroundColor="#cdcdcd";
            for(var k = 0; k<span.length;k++)
            {
                if(k!=j)
                    span[k].style.backgroundColor="white";
            }
        }
        })(i);
    }
</script>

</body>
</html>